﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Text;
using NGCDS.Properties;

namespace NGCDS
{
    class Argument
    {
        private static Settings s = Settings.Default;
        private bool isCheck = false;
        public bool isPraseSuccess = false;
        public List<string> sessionList = new List<string>();

        // config
        public string configFile = s.DEFAULT_CONFIG_FILE_NAME;
        public string logFile = s.DEFAULT_LOGGING_FILE_NAME;

        public void TryPrase(string[] args)
        {
            string arg;
            string errmsg;
            for (int i = 0; i < args.Length; i++)
            {
                arg = args[i];

                if (arg.Equals("--help") || arg.Equals("-h"))
                {
                    printUsage();
                    return;
                }
                else if (arg.Equals("--version"))
                {
                    printVersion();
                    return;
                }
                else if (arg.Equals("-v") || arg.Equals("--verbose"))
                {
                    Globe.isVerbose = true;
                }
                else if (arg.Equals("-d") || arg.Equals("--debug"))
                {
                    Globe.isDebug = true;
                }
                else if (arg.Equals("--log") || arg.Equals("-l") || arg.Equals("-o"))
                {
                    logFile = args[i + 1];
                    i++;
                }
                else if (arg.Equals("--cfg") || arg.Equals("-c") || arg.Equals("-f"))
                {
                    configFile = args[i + 1];
                    i++;
                    // 防呆
                    if (!isFileExists("--cfg -c -f", configFile, out errmsg))
                    {
                        throw new Exception(errmsg);
                    }
                }
                else if (arg.Equals("--check"))
                {
                    isCheck = true;
                }
                else if (arg.StartsWith("-"))
                {
                    errmsg = "Not support argument " + arg;
                    throw new Exception(errmsg);
                }
                else
                {
                    sessionList.Add(arg.ToUpper());
                }
            }

            if (isCheck == true)
            {
                CheckConfig();
                return;
            }

            if (sessionList.Count == 0)
            {
                string msg = "未給定 session name ";
                throw new Exception(msg);
            }

            isPraseSuccess = true;
        }


        private void CheckConfig()
        {
            // 檢查所有 session
            ConfigManager configMGR = new ConfigManager(configFile);
            foreach (string sessionName in configMGR.getSessions())
            {
                Session session = new Session(sessionName, configMGR);
                Console.WriteLine("Check Session: {0}", session.getSessionName());
                if (!session.isWellFormat)
                {
                    Console.WriteLine("Config File 設定錯誤");
                    foreach (string errMessage in session.errMessages)
                    {
                        Console.WriteLine(" - " + errMessage);
                    }
                    Console.WriteLine("Session 設定如下：");
                    Console.WriteLine(session.ToString());
                }
                else
                {
                    Console.WriteLine("Config File 檢查通過");
                }

            }
            Console.WriteLine("檢查完成！");
        }


        private bool isFileExists(string arg, string file, out string errmsg)
        {
            if (File.Exists(file))
            {
                errmsg = "";
                return true;
            }
            else
            {
                errmsg = string.Format("{0} error: file {1} does not Exists.", arg, file);
                return false;
            }
        }


        public void printUsage()
        {
            Settings s = Settings.Default;
            string helpMsg = String.Format(@"
  USAGE: NGCDS [options] session1 [session2] [session3]...

  options:
    -l -o --log <log file>     write message to log file.  DEFAULT = {0}
    -c -f --cfg <config file>  read config file.           DEFAULT = {1}
    --check                    check config file.
                               It's helpful if you change config file. CHECK first!
    -v --verbose               verbose
    -d --debug                 debug
    --version                  show version
    -h --help                  show help
  ", s.DEFAULT_CONFIG_FILE_NAME, s.DEFAULT_LOGGING_FILE_NAME);
            Console.WriteLine(helpMsg);
        }

        private void printVersion()
        {
            Console.WriteLine(new CompiledVersion().ToString());
        }
    }
}
